

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "custom_event.h"
#include "gui.h"

#include "views.h"
#include "views_util.h"

#include "xapp_access.h"
#include "xapp_clock.h"
#include "xapp_battery.h"
#include "xapp_ble.h"
#include "xapp_localize.h"
#include "xapp_profile.h"

#include "log.h"
#define LOG     LOG_INFO0

enum tagWatchfacePreview
{
    tagWatchfacePreviewNext,
    tagWatchfacePreviewLast,
    tagWatchfacePreviewConfirm,
    tagWatchfacePreviewReturn,
};

GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );


const static ui_widgets_type_t wglWatchfacePreview[] = 
{
    // 背景
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),
    // dot
    [1] = uiPictureBox( IMG_Watchface_ViewPage_191_009_01, 0, SIZE_SCALE(60), SIZE_SCALE(210), SIZE_SCALE(127), SIZE_SCALE(6) ),
    // selected
    [2] = uiPictureBox( IMG_Watchface_ViewPageSelected_009_009_01, 0, SIZE_SCALE(60), SIZE_SCALE(210), SIZE_SCALE(6), SIZE_SCALE(6) ),

    
    [3] = uiPanelHorizon( SIZE_SCALE(0), SIZE_SCALE(10), GUI_LCD_RES_H, SIZE_SCALE(179), 3, false ),

    // 图标
    [4] = uiDigital( IMG_Watchface_Preview_269_269_02, -GUI_LCD_RES_H + SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(179), SIZE_SCALE(179) ),
    [5] = uiDigital( IMG_Watchface_Preview_269_269_02, SIZE_SCALE(30),                  SIZE_SCALE(0), SIZE_SCALE(179), SIZE_SCALE(179) ),
    [6] = uiDigital( IMG_Watchface_Preview_269_269_02, GUI_LCD_RES_H + SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(179), SIZE_SCALE(179) ),
};

GUI_VIEW_DEF( viewWatchfacePreview ) = 
{
    // name 
    .name = "WatchfacePreview",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglWatchfacePreview ,
    .widget_cnt = WIDGET_COUNT( wglWatchfacePreview ),

    .flag = GUI_VIEW_FLAG_FUNC
};

#define ITEM_SIZE           (GUI_LCD_RES_H)
#define ITEM_MIN_IDX        (0)
#define ITEM_MAX_IDX        (1)
#define ITEM_COUNT          (2)

#define ITEM_WIDTH          (GUI_LCD_RES_H)

static gui_fling_t*          mp_scroll_fling ;
static int16_t              m_idx_of_first;//, m_idx_of_watchface;
static int32_t m_idx_of_watchface;

static inline int get_next_idx(int idx)
{
   idx ++;
   if( idx > ITEM_MAX_IDX)
   {
       idx = ITEM_MIN_IDX;
   }
   return idx;
}

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = VIEWS_LIFETIME_DEFAULT;

    /*初始化滚动 控制*/
    mp_scroll_fling = gui_fling_get();
    gui_fling_init_t init_obj = GUI_FLING_CONFIG( 0, SIZE_SCALE(10), GUI_LCD_RES_H, SIZE_SCALE(260), true, 0, 0, ITEM_SIZE, 100, NULL );
    gui_fling_init( mp_scroll_fling, &init_obj );

    const profile_system_t* p_profile_sys_desc = xapp_profile_get_system();
    m_idx_of_first = p_profile_sys_desc->watchface - 1;
    if( m_idx_of_first < ITEM_MIN_IDX )
    {
        m_idx_of_first = ITEM_MAX_IDX;
    }
    m_idx_of_watchface = p_profile_sys_desc->watchface;
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
    gui_fling_set_disable( mp_scroll_fling );
    return GUI_DESTROY_RET_DEFAULT; 
}


/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    
    // 当前滚动的总距离，计算得到偏移了几个item 
    int idx = (-mp_scroll_fling->roll_offset_total / ITEM_SIZE); 
    if( idx != 0 )
    {
        mp_scroll_fling->roll_offset_total %= ITEM_SIZE;

        // 计算第一个item值 
        m_idx_of_first += idx;
        if( m_idx_of_first < ITEM_MIN_IDX )
        {
            m_idx_of_first =  ITEM_MAX_IDX ;
        }
        if( m_idx_of_first > ITEM_MAX_IDX )
        {
            m_idx_of_first = ITEM_MIN_IDX ;
        }
    }
    // GUI_LOG3("[Preview]: first = %d, idx = %d, index = %d", m_idx_of_first, idx, VALUES_GET( index ) ); 

    int id = m_idx_of_first;
    prop.digital.idx = id;
    gui_widget_update_prop( &widgets[4], &prop );

    id = get_next_idx(id);
    prop.digital.idx = id;
    gui_widget_update_prop( &widgets[5], &prop );
    m_idx_of_watchface = id; 

    id = get_next_idx(id);    
    prop.digital.idx = id;
    gui_widget_update_prop( &widgets[6], &prop );

    // m_idx_of_watchface = get_next_idx( m_idx_of_first );
    GUI_LOG1("[WF]: index = %d", m_idx_of_watchface ); 
    widgets[2].x = SIZE_SCALE(60) + m_idx_of_watchface * 1; 

    #if 1//defined(BLE_NO_CONNECT)
    widgets[1].visible = widgets[2].visible = 0;
    #endif
    
    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_FLING )
    {
        PROP_ASSIGN_PANEL_ROLL_TOTAL( prop, mp_scroll_fling->item_delta );
        PROP_UPDATE_AT_NO( 3 ); 
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;
    gui_core_lifetime_refresh(menu);
    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DOWN:
        gui_fling_on_touch_press( mp_scroll_fling );
        break;

        case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release( mp_scroll_fling );
        break;
        
        case TP_ALGO_EVT_TYPE_CLICK:
        local_view_event_handler( tagWatchfacePreviewConfirm );
        break;

        case TP_ALGO_EVT_TYPE_HOLD:
        if( touch->deltaX != 0 )
        {
            gui_fling_set_roll( mp_scroll_fling, touch->deltaX );
        }
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_L:        
        local_view_event_handler( tagWatchfacePreviewNext );
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_R:        
        local_view_event_handler( tagWatchfacePreviewLast );
        break;
    }

}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t* kb = (kb_evt_t*)pvParameters;

    switch( kb->type )
    {
        case KB_EVT_CLICK:
        local_view_event_handler( tagWatchfacePreviewReturn );
        break;
    }
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
}

static void local_view_event_handler( uint32_t tag )
{
    switch( tag )
    {
        case tagWatchfacePreviewLast:
        gui_fling_switch_item_extend( mp_scroll_fling, 1, false, GUI_FLING_SWITCH_AVG );
        break;

        case tagWatchfacePreviewNext:
        gui_fling_switch_item_extend( mp_scroll_fling, 1, true, GUI_FLING_SWITCH_AVG );
        break;
        
        case tagWatchfacePreviewConfirm:
        {
            xapp_profile_write_conf( PROFILE_TAG_WATCHFACE, (uint8_t*)&m_idx_of_watchface, 4 );
            gui_view_create( &viewWatchface );
        }
        break;

        case tagWatchfacePreviewReturn:
        if( p_this_parent != NULL )
        {
            gui_view_create_return( p_this_parent );
        }
        else 
        {
            gui_view_create_return( views_load_current_watchface() );
        }
        break;
    }
}

